import state from './state.js'
export default{
	// namespaced:true,
	addToCart(state,goods){
		const findResult=state.carts.find(x=>x.goods_id===goods.goods_id)
		
		if(!findResult){
			state.carts.push(goods)
			
		}else{
			findResult.goods_count++
			
		}
		//使用commit，调用 cart命名空间下的saveToStorage方法
		this.commit('cart/saveToStorage')
	},
	 saveToStorage(state){
		 uni.setStorageSync('cart',JSON.stringify(state.carts) )
	 },
	 
	 changeGoosState(state,goods){
		 const findResult=state.carts.find(x=>x.goods_id===goods.goods_id)
		 
		 if(findResult){
			 findResult.goods_state=goods.goods_state
			 this.commit('cart/saveToStorage')
		 }
	 },
	 changeGoosCount(state,goods){
	 		 const findResult=state.carts.find(x=>x.goods_id===goods.goods_id)
	 		 
	 		 if(findResult){
	 			 findResult.goods_count=goods.goods_count
	 			 this.commit('cart/saveToStorage')
	 		 }
	 },
	 //根据ID删除指定的商品
	 removeGoodsById(state,goods){
		 state.carts=state.carts.filter(x=>x.goods_id!==goods.goods_id)
		 
		 this.commit('cart/saveToStorage')
	 },
	 
	 updateAllGoodsState(state,newState){
	 	return state.carts.forEach(x=>x.goods_state=newState)
	 	this.commit('cart/saveToStorage')
	 }
}